export function parseData(fieldlist, span) {
  let rulesMap = {}
  let filtersMap = {}
  let maprelation = {}
  let fill_item_temp = []
  let fill_item = []
  for (let v of fieldlist) {
    // 绑定字段缺失不处理
    if (!v.b) {
      continue
    }
    maprelation[v.t] = v.b
    // 1 校验规则
    let rulesTempArr = []
    // 1.1 必填
    if (v.isRequired) {
      if (v.type == 'input') {
        rulesTempArr.push({
          required: true,
          message: `${v.comments}不能为空`,
          trigger: 'change',
          pattern: '[^\\s\\r\\n]',
        })
      } else {
        rulesTempArr.push({
          required: true,
          message: `${v.comments}不能为空`,
          trigger: 'change',
        })
      }
    }
    // 1.2 自定义校验规则
    if (v.rules && v.rules.length > 0) {
      v.rules.forEach((r) => {
        rulesTempArr.push({
          message: r.message,
          trigger: 'blur',
          pattern: r.regexp,
        })
      })
    }
    rulesMap[v.t] = rulesTempArr
    // 2. 过滤条件
    if (v.filters && v.filters.length > 0) {
      v.filters.forEach((f) => {
        filtersMap[f.field] = f.value
      })
    }
    // 按行数组装
    fill_item_temp.push(v)
  }
  if (span > 0) {
    recursion_splice(span, fill_item_temp, fill_item)
  }
  // console.log('解析完成', rulesMap, filtersMap)
  return {
    rules: rulesMap,
    filters: filtersMap,
    maprelation: maprelation,
    fill_item: fill_item
  }

}

// 表格解析
export function parseSubtable(fieldlist) {
  let columns = []
  columns.push({
    title: '序号',
    width: '65px',
    height: '50px',
    fixed: 'left',
    scopedSlots: {
      customRender: 'serial'
    },
  })
  for (let v of fieldlist) {
    // 绑定字段缺失不处理
    if (!v.b) {
      continue
    }
    // 添加子表格配置
    columns.push({
      title: v.comments,
      dataIndex: v.t,
      width: '1',
      align: 'left',
      scopedSlots: {
        customRender: v.t + 'Slot'
      },
    })
  }
  if (columns.length > 5) {
    columns.forEach(v => {
      if (v.width == '1') {
        v.width = '200px'
      }
    })
  }
  columns.push({
    title: '操作',
    dataIndex: 'action',
    width: '65px',
    fixed: 'right',
    scopedSlots: {
      customRender: 'operation'
    },
  })
  return columns
}

// 递归, 按两个一组
function recursion_splice(span, spanArr, tempArr) {
  if (spanArr.length > span) {
    tempArr.push(spanArr.splice(0, span))
    if (spanArr.length > span) {
      recursion_splice(span, spanArr, tempArr)
    } else {
      if (spanArr.length > 0) {
        tempArr.push(spanArr)
      }
      return
    }
  } else {
    tempArr.push(spanArr)
  }
}